﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmDangKyMaCaBiet : DevExpress.XtraEditors.XtraForm
    {
        #region khai báo bien
        public string maTaiLieu = "";
        TextMessageBox box = new TextMessageBox();
        DataTable dt = new DataTable();
        DangKyCaBiet_BLL dk_bll = new DangKyCaBiet_BLL();
        private string tienTo = "";
        private string chuoi = "";
        private int lengthTienTo = 0;
        private int fromNo=0;
         private int ToNo = 0;
         private bool check=true;
        #endregion

        #region Loadform

        public frmDangKyMaCaBiet()
        {
            InitializeComponent();
        }

        private void frmDangKyMaCaBiet_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region cac chuc nang

        private void btnTaoMa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            fromNo=box.ToInt(txtFromNo.Text);
            ToNo=box.ToInt(txtToNo.Text);
            
            if (checkValid())
            {
                DangKyCaBiet_Public dk = new DangKyCaBiet_Public();
                for (int i = fromNo; i < ToNo; i++)
                {
                    dk.SoCaBiet = box.AutoID(tienTo,tienTo.Length, dk_bll.TaoBang("select SoCaBiet from DangKyCaBiet where SoCaBiet like '"+tienTo+"'+'%'"));
                    dk.MaTaiLieu = txtMaTaiLieu.Text.Trim();
                    dk.NgayDangKy = box.GetSystemDate();
                    dk.TrangThaiSuDung = check;
                    dk.TrangThaiMuon = false;
                    dk.MaNhanVien = box.MaNhanVien;
                    dk_bll.Insert_DangKyTaoMaCaBiet(dk);
                }
                XtraMessageBox.Show("Tạo Mã cá biệt cho '"+ToNo+"' tài liệu thành công!",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
        #endregion

        #region cac su kien

        private void frmDangKyMaCaBiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        private void txtMaTaiLieu_Leave(object sender, EventArgs e)
        {
            try
            {
                dt = dk_bll.TaoBang("select a.NhanDe,a.SoLuong,b.TenLinhVuc from TaiLieu a join DM_LinhVuc b on a.MaLinhVuc=b.MaLinhVuc where MaTaiLieu=N'" + txtMaTaiLieu.Text.Trim() + "' and TrangThaiDKCB=0");
                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    txtTenTaiLieu.Text = dr["NhanDe"].ToString();
                    txtFromNo.Text = "1";
                    txtToNo.Text = dr["SoLuong"].ToString();

                    //lấy tiền tố
                    chuoi=dr["TenLinhVuc"].ToString();
                    string[] _splip =chuoi.Split(' ');
                    for (int i = 0; i < _splip.Length; i++)
                    {
                        

                        tienTo += _splip[i].Substring(0, 1);
                    }
                    txtTienTo.Text = tienTo;
                    //lengthTienTo = tienTo.Length;
                }
            }
            catch (Exception)
            { }
        }
        private void ckbTrangThai_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTrangThai.CheckState == CheckState.Checked)
                check = true;
            else check = false;
        }
        #endregion

        #region ham viet them

        private bool checkValid()
        {
            if (txtMaTaiLieu.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Mã Tài Liệu.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtMaTaiLieu.Focus();
                return false;
            }
            return true;
        }
        private void LoadData()
        {
            if (maTaiLieu != "")
            {
                dt = dk_bll.TaoBang("select a.NhanDe,a.SoLuong,b.TenLinhVuc from TaiLieu a join DM_LinhVuc b on a.MaLinhVuc=b.MaLinhVuc where MaTaiLieu=N'" + maTaiLieu + "' and TrangThaiDKCB=0");
                DataRow dr = dt.Rows[0];
                txtMaTaiLieu.Text = maTaiLieu;
                txtTenTaiLieu.Text = dr["NhanDe"].ToString();
                txtFromNo.Text = "1";
                txtToNo.Text = dr["SoLuong"].ToString();

                //lấy tiền tố
                chuoi = dr["TenLinhVuc"].ToString();
                string[] _splip = chuoi.Split(' ');
                for (int i = 0; i < _splip.Length; i++)
                {


                    tienTo += _splip[i].Substring(0, 1);
                }
                txtTienTo.Text = tienTo;
                //lengthTienTo = tienTo.Length;
            }
        }
        #endregion





    }
}